use  case person sib06 sib07 sib11 using "${Input}\mhd3_dta\mhdsib2.dta"
sort case person
save ${Output}\Siblings, replace



/***** need to figure out which brothers and sisters were alive at the time of marriage and so we need at at marriage and year of marriage *******/ 
use ${Output}\Siblings
sort case person
merge case person using ${Output}\BasicMarr, nokeep
drop _merge
sort case person
merge case person using ${Output}\FirstMarriageInfo, nokeep
drop _merge
save, replace
sort case person
rename person lh16 
sort case lh16
merge case lh16 using ${Output}\AgeMarriage, nokeep
drop _merge
rename lh16 person 
sort case person 
save ${Output}\Siblings, replace

use case person lh12y using ${Output}\Roster
sort case person
save ${Output}\Ages, replace 

use ${Output}\Siblings
sort case person
merge case person using ${Output}\Ages, nokeep
drop _merge
sort lh12y 
sort case person
rename lh12y Age

generate YearMarried=FirstMarrmh06y
replace YearMarried=. if FirstMarrmh06y>96
generate AgeMarried=FirstMarrmh07
replace AgeMarried=. if FirstMarrmh07>96
replace AgeMarried=. if Age<AgeMarried

generate SpAgeMarried=SpFirstMarrmh07 
generate SpYearMarried=96-SpAge+SpAgeMarried 

generate YrsMarried=96-YearMarried
replace YrsMarried=96-SpYearMarried if YearMarried==.

replace AgeMarried=Age-YrsMarried if AgeMarried==. 
replace AgeMarried=. if Age<AgeMarried
replace YearMarried=96-Age+AgeMarried if YearMarried==.
replace YearMarried=96-Age+AgeMarried if YearMarried==.
drop if YearMarried>96
generate MaleSibs=(sib06==1)
generate FemaleSibs=(sib06==3)
replace sib11=. if sib11==99
generate YrSibBorn=96-sib11 
generate BroAtMarr=(sib06==1 & YrSibBorn<YearMarried)
generate SisAtMarr=(sib06==3 & YrSibBorn<YearMarried)
generate OlderFemaleSibs=(sib06==3 & sib11>Age)
generate OlderMaleSibs=(sib06==1 & sib11>Age)
generate OlderSibs=(sib11>Age)
generate YoungerSibs=(sib11<=Age)

collapse (sum) MaleSibs FemaleSibs OlderFemaleSibs OlderMaleSibs OlderSibs YoungerSibs BroAtMarr SisAtMarr, by (case person)
sort case person
save ${Output}\SexSibs, replace


use ${Output}\MainFile
sort case person
merge case person using ${Output}\SexSibs, nokeep
drop _merge
sort case person
save ${Output}\MainFile, replace

erase ${Output}\SexSibs.dta
erase ${Output}\Roster.dta
erase ${Output}\FirstMarriageInfo.dta
erase ${Output}\AllMarriageInfo.dta
